//
//  PlaybackViewController.m
//  JXW_UnionSDK
//
//  Created by 彭月辉 on 2022/6/22.
//

#import "PlaybackViewController.h"
#import "Masonry.h"
#import <MobileVLCKit/MobileVLCKit.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <ADSEFramework/ADSELinkManager.h>
#import "SVProgressHUD.h"
#import <AFNetworking/AFNetworking.h>

@interface PlaybackViewController ()<VLCMediaPlayerDelegate>{
    UIView *playbackView;
    UIView *progressView;
    UIImageView *imageview;
    VLCMediaPlayer *vlcPlayer;
    
    UILabel *currenttimeLabel;
    UILabel *totaltimeLabel;
    UISlider *progressSlider;
    
    UIButton *playBtn;
    UIButton *downloadBtn;

}

@end

@implementation PlaybackViewController

//- (void)viewDidLoad {
//    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
//    self.title = self.item.name;
//    [self initUI];
//    [self initPlayer];
//}
//
//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//}
//
//- (void)viewWillDisappear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [vlcPlayer stop];
//}
//
//- (void)initPlayer{
//    if(self.localURL){
//        downloadBtn.hidden = YES;
//        if(self.localType == LINK_DIR_PHOTO){
//            playbackView.hidden = YES;
//            progressView.hidden = YES;
//            playBtn.hidden = YES;
//            imageview.hidden = NO;
//            imageview.image = [UIImage imageWithContentsOfFile:self.localURL];
//        }
//        else{
//            playbackView.hidden = NO;
//            progressView.hidden = NO;
//            playBtn.hidden = NO;
//            imageview.hidden = YES;
//            [self initVLCPlayerWithLocalPath:self.localURL];
//        }
//    }
//    else{
//        downloadBtn.hidden = NO;
//
//        if([self.item.type integerValue] == LINK_DIR_PHOTO){
//            playbackView.hidden = YES;
//            progressView.hidden = YES;
//            playBtn.hidden = YES;
//            imageview.hidden = NO;
//            [imageview sd_setImageWithURL:[NSURL URLWithString:self.item.onlineURL]];
//        }
//        else{
//            playbackView.hidden = NO;
//            progressView.hidden = NO;
//            playBtn.hidden = NO;
//            imageview.hidden = YES;
//            [self initVLCPlayer:self.item.onlineURL];
//        }
//    }
//}
//
//- (void)playAciton:(UIButton *)sender{
//    if(sender.selected){
//        [vlcPlayer pause];
//    }
//    else{
//        if(vlcPlayer.state == VLCMediaPlayerStateStopped){
//            [self initPlayer];
//        }
//        else{
//            [vlcPlayer play];
//        }
//    }
//}
//
//- (void)downloadFile{
//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
//    /* 创建网络下载对象 */
//    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//    
//    /* 下载地址 */
//    NSURL *url = [NSURL URLWithString:self.item.downloadURL];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    
//    /* 下载路径 */
//    NSString *main = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
//    NSString *path;
//    if([self.item.type integerValue] == LINK_DIR_PHOTO){
//        path = [main stringByAppendingPathComponent:@"photo"];
//    }
//    else if([self.item.type integerValue] == LINK_DIR_VIDEO){
//        path = [main stringByAppendingPathComponent:@"video"];
//    }
//    else if([self.item.type integerValue] == LINK_DIR_EVENT){
//        path = [main stringByAppendingPathComponent:@"video_emergency"];
//    }
//
//    if(![[NSFileManager defaultManager] fileExistsAtPath:path]){
//        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
//    }
//    NSString *filePath = [path stringByAppendingPathComponent:self.item.name];
//    
//    /* 开始请求下载 */
//    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            NSLog(@"下载进度：%.0f％", downloadProgress.fractionCompleted * 100);
//            [SVProgressHUD showProgress:downloadProgress.fractionCompleted status:self.item.name];
//        });
//    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            //如果需要进行UI操作，需要获取主线程进行操作
//        });
//        return [NSURL fileURLWithPath:filePath];
//                
//    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//         NSLog(@"下载完成");
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self showError:@"下载完成"];
//            [SVProgressHUD dismiss];
//        });
//    }];
//     
//    [downloadTask resume];
//}
//
//#pragma mark - VLCMediaPlayerDelegate
//
//- (void)mediaPlayerStateChanged:(NSNotification *)aNotification{
//    NSLog(@"VLC state --> %ld",vlcPlayer.state);
//    if(vlcPlayer.state == VLCMediaPlayerStatePlaying || vlcPlayer.state == VLCMediaPlayerStateBuffering){
//        playBtn.selected = YES;
//    }
//    else if(vlcPlayer.state == VLCMediaPlayerStateStopped){
//        currenttimeLabel.text = @"00:00";
//        progressSlider.value = 0;
//        playBtn.selected = NO;
//    }
//    else{
//        playBtn.selected = NO;
//    }
//}
//
//
//-(void)mediaPlayerTimeChanged:(NSNotification *)aNotification{
//    
//    int currentTime = ceilf(vlcPlayer.time.intValue/1000);
//    int totalTime = vlcPlayer.media.length.intValue/1000;
//    
//    NSLog(@"%d/%d",currentTime,totalTime);
//    float progress = currentTime *1.0/totalTime;
//
//    currenttimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",currentTime/60,currentTime%60];
//    totaltimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",totalTime/60,totalTime%60];
//    progressSlider.value = progress;
//
//}
//
//
//- (void)initVLCPlayer:(NSString *)url{
//    vlcPlayer = [[VLCMediaPlayer alloc] initWithOptions:nil];
//    vlcPlayer.delegate = self;
//    vlcPlayer.drawable = playbackView;
//    vlcPlayer.media = [VLCMedia mediaWithURL:[NSURL URLWithString:url]];
//    [vlcPlayer play];
//}
//
//- (void)initVLCPlayerWithLocalPath:(NSString *)path{
//    NSArray *options = [NSArray arrayWithObject:@"--rtsp-tcp"];
//    vlcPlayer = [[VLCMediaPlayer alloc] initWithOptions:options];
//    vlcPlayer.delegate = self;
//    vlcPlayer.drawable = playbackView;
//    vlcPlayer.media = [VLCMedia mediaWithPath:path];
//    [vlcPlayer play];
//}
//
//- (void)initUI{
//    playbackView = [[UIView alloc] init];
//    [self.view addSubview:playbackView];
//    
//    playbackView.backgroundColor = [UIColor blackColor];
//    
//    progressView = [[UIView alloc] init];
//    [self.view addSubview:progressView];
//    
//    progressView.backgroundColor = [UIColor blackColor];
//    
//
//    imageview = [[UIImageView alloc] init];
//    [self.view addSubview:imageview];
//    
//    imageview.backgroundColor = [UIColor blackColor];
//    imageview.contentMode = UIViewContentModeScaleAspectFit;
//    
//    
//    currenttimeLabel = [[UILabel alloc] init];
//    [progressView addSubview:currenttimeLabel];
//    
//    currenttimeLabel.textColor = [UIColor whiteColor];
//    currenttimeLabel.textAlignment = NSTextAlignmentRight;
//    
//    totaltimeLabel = [[UILabel alloc] init];
//    [progressView addSubview:totaltimeLabel];
//    
//    totaltimeLabel.textColor = [UIColor whiteColor];
//    totaltimeLabel.textAlignment = NSTextAlignmentLeft;
//
//
//    progressSlider = [[UISlider alloc] init];
//    [progressView addSubview:progressSlider];
//    
//    progressSlider.userInteractionEnabled =  NO;
//
//    playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.view addSubview:playBtn];
//    
//    
//    [playBtn setBackgroundColor:[UIColor lightGrayColor]];
//    [playBtn setTitle:@"播放" forState:UIControlStateNormal];
//    [playBtn setTitle:@"暂停" forState:UIControlStateSelected];
//    [playBtn setTitleColor:[UIColor blueColor] forState:normal];
//    [playBtn setTitleColor:[UIColor purpleColor] forState:UIControlStateSelected];
//    [playBtn addTarget:self action:@selector(playAciton:) forControlEvents:UIControlEventTouchUpInside];
//    
//    
//    downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.view addSubview:downloadBtn];
//
//    [downloadBtn setImage:[UIImage imageNamed:@"FileDownload"] forState:normal];
//    [downloadBtn addTarget:self action:@selector(downloadFile) forControlEvents:UIControlEventTouchUpInside];
//
//    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:downloadBtn];
//    self.navigationItem.rightBarButtonItem = rightItem;
//
//    [playbackView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_topLayoutGuideBottom);
//        make.left.right.equalTo(@0);
//        make.height.equalTo(@300);
//    }];
//    
//    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(playbackView.mas_bottom);
//        make.left.right.equalTo(@0);
//        make.height.equalTo(@30);
//    }];
//
//    
//    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_topLayoutGuideBottom);
//        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
//        make.left.right.equalTo(@0);
//    }];
//
//    [progressSlider mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(progressView);
//        make.width.equalTo(@200);
//        make.height.equalTo(@20);
//    }];
//
//    
//    [currenttimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(progressSlider);
//        make.right.equalTo(progressSlider.mas_left).offset(-10);
//        make.width.equalTo(@100);
//        make.height.equalTo(@30);
//    }];
//    
//    [totaltimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(progressSlider.mas_right).offset(10);
//        make.centerY.width.height.equalTo(currenttimeLabel);
//    }];
//
//    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.view);
//        make.top.equalTo(progressView.mas_bottom).offset(50);
//        make.width.equalTo(@150);
//        make.height.equalTo(@50);
//    }];
//    
//}
//
@end
